<?php
/**
 * Created by PhpStorm.
 * User: lauwen
 * Date: 2017/10/24
 * Time: 19:39
 *
 *          ┌─┐       ┌─┐
 *       ┌──┘ ┴───────┘ ┴──┐
 *       │                 │
 *       │       ───       │
 *       │  ─┬┘       └┬─  │
 *       │                 │
 *       │       ─┴─       │
 *       │                 │
 *       └───┐         ┌───┘
 *           │         │
 *           │         │
 *           │         │
 *           │         └──────────────┐
 *           │                        │
 *           │                        ├─┐
 *           │                        ┌─┘
 *           │                        │
 *           └─┐  ┐  ┌───────┬──┐  ┌──┘
 *             │ ─┤ ─┤       │ ─┤ ─┤
 *             └──┴──┘       └──┴──┘
 *                 神兽保佑
 *                 代码无BUG!
 *"The trouble with programmers is that you can never tell what a programmer is doing until it’s too late.”- Seymour Cray
 */
namespace Admin\Controller;
use Think\Controller;

class UserController extends Controller{
    /**
     * Login page
     */
    public function login(){
        $this->display();
    }

    /**
     * user login info check
     */
    public function loginCheck(){
        $userName = $_POST['username'];
        $passWord = $_POST['password'];

        if(isset($userName) && isset($passWord) && !empty($passWord) && !empty($userName)){
            $User = M('user_info');
            $userInfo = $User -> where(array("name" => $userName,"pwd" => $passWord)) -> find();
//            dump($userInfo);die;
            if($userInfo){
                $_SESSION['loginStatus'] = $userName;
                $_SESSION['userGrade'] = $userInfo['usergrade'];
//                dump($_SESSION);die;
                $this->success("登录成功，跳转中……",U("Admin/Index/index"),3);
            }else{
                $this -> error("密码或用户名错误，请重新登录……");
            }
        }else{
            $this -> error("登录数据提交失败，请重新登录……");
        }
    }
    public function loginStatusCheck($grade){
        if((isset($_SESSION['loginStatus'])) && ($_SESSION['loginStatus'] != '')){
            if($_SESSION['userGrade'] <= $grade){
                return true;
            }else{
                $this->error('你无权访问该页面');
            }
        }else{
            $this->error('请登录……',U('User/login'),3);
        }
    }

    public function exitLogin(){
        $_SESSION['loginStatus'] = null;
        $_SESSION['userGrade'] = null;
        $this->success('已退出登录！','login',3);
    }

}

